package com.ziyun.pop.modules.storeMaterials.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyun.pop.modules.storeMaterials.dao.MaterialDao;
import com.ziyun.pop.modules.storeMaterials.entity.ErpStockEntity;
import com.ziyun.pop.modules.storeMaterials.entity.MaterialEntity;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import com.ziyun.pop.modules.storeMaterials.service.MaterialService;
import io.renren.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/10/18 14:38
 * @Version V1.0
 */
@Service("materialService")
public class MaterialServiceImpl extends ServiceImpl<MaterialDao,MaterialEntity> implements MaterialService{

	@Autowired
	private ErpStockService erpStockService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		String key = "categoryId";
		String key2 = "materialName";
		if(params.containsKey(key)&&params.containsKey(key2)&&params.get("categoryId")!=""){
			Iterator<String> iterator = params.keySet().iterator();
			while(iterator.hasNext()){
				String kesy = iterator.next();
				if("materialName".equals(kesy)){
					iterator.remove();
				}
			}
		}
		boolean contains2 = params.containsKey(key2);
		if(contains2){
			Object s = params.get("sellerId");
			long sellerId = Long.valueOf(String.valueOf(s)).longValue();
			Integer stockId = erpStockService.getStockId(sellerId);
			params.put("stockId",stockId);
			int totalCount = 0;
			Object obj = params.get("materialName");
			String materialName = obj.toString();
			totalCount = baseMapper.getqueryCountByname(materialName);
			int current = Integer.valueOf((String) params.get("page"));
			int size = Integer.valueOf((String) params.get("limit"));
			params.put("offset", (current - 1) * size);
			params.put("limit", size);
			List<MaterialEntity> list = baseMapper.getQueryListByname(params);
			return new PageUtils(list, totalCount, size, current);
		}
		boolean contains = params.containsKey(key);
		if (contains) {
			Integer categoryId = null;
			int totalCount = 0;
			Object obj = params.get("categoryId");
			int BM = 1;
			int BT = Integer.parseInt(String.valueOf(obj));
			int BJ = 0;
			if(BT == BJ){
				Object s = params.get("sellerId");
				long sellerId = Long.valueOf(String.valueOf(s)).longValue();
				Integer stockId = erpStockService.getStockId(sellerId);
				params.put("stockId",stockId);
				int i = baseMapper.getqueryCountBycategoryId();
				int current = Integer.valueOf((String) params.get("page"));
				int size = Integer.valueOf((String) params.get("limit"));
				params.put("offset", (current - 1) * size);
				params.put("limit", size);
				List<MaterialEntity> list = baseMapper.getQueryListBycategoryId(params);
				return new PageUtils(list, i, size, current);
			}
			Object s = params.get("sellerId");
			long sellerId = Long.valueOf(String.valueOf(s)).longValue();
			Integer stockId = erpStockService.getStockId(sellerId);
			params.put("stockId",stockId);
			totalCount = baseMapper.getqueryCount(categoryId);
			int current = Integer.valueOf((String) params.get("page"));
			int size = Integer.valueOf((String) params.get("limit"));
			params.put("offset", (current - 1) * size);
			params.put("limit", size);
			List<MaterialEntity> list = baseMapper.getQueryList(params);
			return new PageUtils(list, totalCount, size, current);
		} else {
			Object s = params.get("sellerId");
			long sellerId = Long.valueOf(String.valueOf(s)).longValue();
			Integer stockId = erpStockService.getStockId(sellerId);
			params.put("stockId",stockId);
			int totalCount = baseMapper.getqueryCount2();
			int current = Integer.valueOf((String) params.get("page"));
			int size = Integer.valueOf((String) params.get("limit"));
			params.put("offset", (current - 1) * size);
			params.put("limit", size);
			List<MaterialEntity> list = baseMapper.getQueryList2(params);
			return new PageUtils(list, totalCount, size, current);
		}
	}

	@Override
	public MaterialEntity selectById(Integer materialId) {
		return baseMapper.selectByMaterialId(materialId);
	}

	@Override
	public void updateMaterialAllColumnById(Map<String, Object> map) {
		baseMapper.updateMaterial(map);
	}

	@Override
	public ErpStockEntity getLoginErpStock(Long sellerId) {
		return this.baseMapper.getLoginErpStock(sellerId);
	}
}


